import {h,render} from 'vue'
import ConfirmDialog from '@/components/dialogs/ConfirmDialog.vue';
import DialogBasic from '@/components/dialogs/DialogBasic.vue';
import PluginContext from './PluginContext';


export default class Dialogs {
   static initContext() {
    PluginContext.getApp().config.globalProperties.$dialog = {
      confirm: confirmDialog,
      alert: alertDialog
    };
  }
}

export const confirmDialog = (content) =>{
        try {
          const div = document.createElement('div');
      
          return new Promise((resolve, reject) => {
            const props = {
              confirmationQuestion: content,
              onCloseDialog: () => {
                reject('确认框已取消');
              },
              onConfirm:()=>{
                resolve()
              }
            };
            const vNode = h(ConfirmDialog,props);
            vNode.appContext = PluginContext.getApp()._context;
            render(vNode, div);
            
          })
        } catch (err) {
          console.error(`[Vuetify3Dialog] ${err.message} [${err.stack}]`);
        }
}


export const alertDialog = (title,content) =>{
  try {
    const div = document.createElement('div');

    return new Promise((resolve, reject) => {
      const props = {
        title: title,
        content: content,
        onConfirm:()=>{
          resolve()
        }
      };
      const vNode = h(DialogBasic,props);
      vNode.appContext = PluginContext.getApp()._context;
      render(vNode, div);
      
    })
  } catch (err) {
    console.error(`[Vuetify3Dialog] ${err.message} [${err.stack}]`);
  }
}

